package com.loanapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.loanapp.domain.LoanApplication;
import com.loanapp.service.LoanApplicationService;

public class LoanApplicationController extends SimpleFormController {

	private static Log log = LogFactory.getLog(LoanApplicationController.class);

	private LoanApplicationService service = new LoanApplicationService();

	public LoanApplicationController() {
		setCommandClass(LoanApplication.class);
	}

	public void setLoanApplicationService(LoanApplicationService service) {
		this.service = service;
	}

	protected ModelAndView showForm(HttpServletRequest request,
            HttpServletResponse response, BindException errors) throws Exception {
		log.debug("Enter showForm().");

		return new ModelAndView(getFormView());
	}

	protected ModelAndView processFormSubmission(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
		log.debug("Enter processFormSubmission().");

		return onSubmit(request, response, command, errors);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		log.debug("Enter onSubmit().");
		LoanApplication loanApp = (LoanApplication)command;
		log.debug("LoanApplication:\n"+loanApp.toString());

		service.sendAUSCreditRequest(loanApp);

		return new ModelAndView(getSuccessView());
	}
}
